// const { ajax } = require("jquery");

$('.paer_logo').on('click', function(){
  history.back(-1)
})


let res = "";
let res2 = ""
let data_sync = async () => {
  let data = await ajax({
    type: "GET",
    url: "http://127.0.0.1:4000/tested/GetTested",
    data: undefined,
  });
  
  res = JSON.parse(data);
  let stu = window.location.search.slice(window.location.search.indexOf("?")+1,window.location.search.indexOf('&'))
  let stu_id = window.location.search.slice(window.location.search.indexOf('&')+1)
   
  let data_res =  res.obj.filter(v=>{
    
   return  v.studentId._id==stu&&stu_id==v.testId._id
  //  console.log(v.studentId._id)
  // console.log(v.testId._id)
  })
 
  let data2 = await ajax({
    type: "GET",
    url: "http://127.0.0.1:4000/testPaper/GetTestPaperFind",
    data: undefined,
  })
  res2 = JSON.parse(data2);
  // console.log(res2)
 let data_f= res2.obj.filter(v=>{
    return v._id ==stu_id
  })
console.log(data_f[0])

  console.log(data_res[0].testId)
  console.log(res)
  if (res.code == 200) {
    var swiper = new Swiper({
      obj: ".swiper_content",
      arr: data_f[0],     //改
      width: 786,
      height: 400,
      autoPlay: false,
      delay: 2000,
     
    });
    swiper.xuan();

    $(".di").html("第1题");
    // $(".gong").html(`共${res.obj[0].exercisesId.length}题`);  //改
    table_xuan(data_f[0].exercisesId); //渲染选项卡   //改
    

    // $('.swiper_item').each((i,v)=>{
    //   console.log($(v).find('.da_your span').html())
    //   // console.log($(v).find('.da_your_s span').html())
    //   // if($(v).find('.da_your span').html()==$(v).find('.da_your_s span').html()){
       
    //   // }
    // })

    //改
    // console.log(res.obj[0].exercisesId) //题目
  


     $('.test_title').html(res.obj[0].title)   //改
  
     let stu2 = window.location.search.slice(window.location.search.indexOf("?")+1,window.location.search.indexOf('&'))
     let stu_id2 = window.location.search.slice(window.location.search.indexOf('&')+1)
     let ans = JSON.parse(localStorage.getItem(`${stu2}${stu_id2}`))
 
     let da = []
     ans.forEach(v=>{
 
      

        $('.swiper_item').each((i,a)=>{
            if(v.id==$(a).attr('item_id')){

                $(a).find(".da_your span").html(zhuan(v.ans))
              
              
                $(a).find(`.en_no[sid=${v.ans}]`).css({
                    background:'red',
                    color:'white'
                })
            }
        })


      //     console.log()
       let ee= data_f[0].exercisesId.filter(e=>{
          return  v.id == e._id&&v.ans !== e.answer
        })
     
        da.push(ee[0])
       
     })


 
     da.forEach(v=>{
        $('.swiper_item').each((i,a)=>{
            if(v._id== ($(a).attr('item_id'))&&v.answer!==v.ans){
              
              $(`.item_D[index=${i}]`).css({
                background:'red',
                color:'white'
              })

              
              $(a).find(".da_your").css({
             color:'red'
                })
            
            }
        })
     })


 //判断错题
 let wrongs = []
     $('.swiper_item').each((i,v)=>{
      wrongs.push(v)
            if($(v).find(".da_your span").html()==$(v).find(".da_your_s span").html()){
             
              $(v).find(".da_your").css({
                color:'rgba(23,150,219)'
              })
             let index = $(v).find(".da_your_s").attr('index')
             $(v).find(`.en_no[sid=${index}]`).css({
               background:'rgba(23,150,219)',
             })

             let tavle_index = $(v).attr('sum')

             $(`.item_D[index=${tavle_index}]`).css({
               background:'rgba(23,150,219)'
             })
            }
     })
     $('.w_d span:eq(1)').html(`错${wrongs.length}道题`)
  }
};




data_sync();

function zhuan(num){
    if(num== "0"){
        return "A"
    }else if(num =="1"){
       return "B"
    }
    else if(num =="2"){
       return "C"
    }
    else if(num =="3"){
       return "D"
    }
}

//渲染选项卡

//点击交卷
$(".jiao_btn_juan").on("click", function () {
  if ($(".item_D").length == $(".zhong").length) {
    //  提交卷子
    console.log(111);
  } else {
    alert("您还没有答完卷子，不能提交");
  }
});

// $(".ti_content").on("click", function (e) {
//   if (e.target.className == "en_no") {
//     e.target.className = "en_check";

//     $(e.target).parents().siblings().find(".en_check").attr("class", "en_no");
//   } else if (e.target.className == "en_check") {
//     e.target.className = "en_no";
//   }
// });


//点击下一题
$('.btn_test').on('click', function(){
  setTimeout(function(){
    let i = $('.swiper').attr("index");
    // console.log(i)
   $('.di').html(`第${parseInt(i)+1}题`)
  },200)
  
   

 
})



//点击下一题
// let arr = [];
// let arr2 = [];
// $(".btn_test").on("click", function (e) {
//   let obj = {};
//   if (
//     $(".ti_content").find(".en_check").parent().parent().parent().length > 0
//   ) {
//     let id = $(".ti_content")
//       .find(".en_check")
//       .parent()
//       .parent()
//       .parent()
//       .attr("item_id");
//     let ans = $(".ti_content").find(".en_check").attr("sid");
//     obj["id"] = id;
//     obj["ans"] = ans;
//     arr.push(obj);
//   }
//   //把写好的题存到local  //学生名字  做的题目
//   localStorage.setItem(`610bb559a50c000097006402`,JSON.stringify(arr))


//   //点击下一题 获取错题
//   let arr3 = [];
//   arr.forEach((e) => {
//     arr2 = res.obj[0].exercisesId.filter((v) => {   //改
   
//       if (e.id == v._id && v.answer !== parseInt(e.ans)) {
//         return v;
//       }
//     });
   
//   });
 
// });




function table_xuan(arr) {
  console.log(arr)
  let str = "";
  arr.forEach((v, i) => {
    var item = `
   <div index= ${i} class="item_D fl_left">
   ${i + 1}
 </div>
   `;
    str += item;
  });
  document.querySelector(".table_d_4").innerHTML = str;
}
